<?php
defined('_JEXEC') or die ('Acesso restringido');
jimport('joomla.application.component.model');

class DeportesModelTipos extends JModel {

	function guardar_tipos($data){
		
   		$row =& JTable::getInstance('tipos','Table');
   			
    	// Bind the form fields to the hello table
    	if (!$row->bind($data)) {
	        $this->setError($this->_db->getErrorMsg());
        	return false;
    	}
 
	    // Make sure the hello record is valid
	    if (!$row->check()) {
        	$this->setError($this->_db->getErrorMsg());
        	return false;
    	}
 
	    // Store the web link table to the database
	    if (!$row->store()) {
    	    $this->setError($this->_db->getErrorMsg());
        	return false;
    	}
    	return true;
	}


    function obtenerTipos($tabla=null,$idtipo=null){
	     $db = &JFactory::getDBO();
	     $sql="SELECT idtipo,tipo,tabla,idestado FROM #__tipos WHERE ";
     	$sql.=" idtipo is not null  ";
     	if($tabla) $sql.=" AND tabla='$tabla' ";
     	if($idtipo) $sql.=" AND idtipo in ($idtipo) ";
       		else $sql.=" and idestado=1 ";
     	$sql.=" ORDER BY TIPO ";
     	$db->setQuery($sql);
     	return $db->loadObjectList();
    }

	
	function selectEstados($name='idestado',$idestado=null){
       $options[] = JHTML::_('select.option',1,'Activo','idestado','estado');
       $options[] = JHTML::_('select.option',2,'Inactivo','idestado','estado');
       return JHTML::_('select.genericlist',$options,$name, 'class="inputbox"','idestado','estado', $idestado);
    }
	
	 
  public function datos_grid(){
	 	
 	$db = &JFactory::getDBO();
    $pagination = false;
	if (isset($_REQUEST["page"])) {
	   $pagination = true;	
	   $page = intval($_REQUEST["page"]);
	   
	}else{
		$page=0;
	}

	if(isset($_REQUEST["perpage"]))
		$perpage = intval($_REQUEST["perpage"]);
	else
		$perpage = 10;
		
		
	// this variables Omnigrid will send only if serverSort option is true
    if(isset($_REQUEST["sorton"])) { $sorton = $_REQUEST["sorton"]; }else{ $sorton = null; }
	if(isset($_REQUEST["sortby"])) { $sortby = $_REQUEST["sortby"]; }else{ $sortby = null; }
	
	$n = ( $page -1 ) * $perpage;
        $limit = "";
	
	if ( $pagination )
		$limit = "LIMIT $n, $perpage";
	
	$sql= "SELECT CONCAT('<input type=checkbox value=',idtipo,' name=checks ></input>') as sele" .
			",tipo " .
			",CASE WHEN idestado=1 then 'ACTIVO' ELSE 'INACTIVO' END as estado".
			",CONCAT('<a href=javascript:void(0); onclick=editar_tipo(',idtipo,') >Editar</a>') as editar " .
			" FROM #__tipos where tabla='deportes' ";
			
	if($sorton) $sql.=" ORDER BY $sorton $sortby ";
	
	if($limit) $sql.=" $limit ";
	
	//echo $sql;exit;
	
        $db->setQuery($sql);
        $db->query();
        $total = $db->getNumRows();
        $row = $db->loadObjectList();
	
	$row = array("page"=>$page, "total"=>$total, "data"=>$row);

	echo json_encode($row);
 }
	
	
	public function eliminarTipos($idtipo){
   		$db = &JFactory::getDBO();
     	$sql="delete from  #__tipos where idtipo in ($idtipo) ";
     	$db->setQuery($sql);
     	if($db->query()){
	     	return true;
     	}else{
	     	return false;
    	} 
   }
   
}